const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')  //css文件提取，生产环境使用
const HtmlWebpackPlugin = require('html-webpack-plugin')

const devMode = process.env.NODE_ENV === 'development'  //环境判断

module.exports = {
    entry: {
        main: './src/index.js'
    },
    plugins: [
        new HtmlWebpackPlugin({  //html模板文件
            template: path.resolve('public/index.html'),
            title: 'react-admin-template',
            favicon: path.resolve('public/favicon.ico')
        })
    ],
    resolve: {
        extensions: ['.js', '.jsx'],
        alias: {
            '@': path.resolve('src')  //别名，@即代表src文件夹
        }
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                include: path.resolve('src'),  //只在src下的文件执行loader
                loader: 'babel-loader'
            },
            {
                test: /\.css$/,
                use: [
                    devMode ? 'style-loader' : MiniCssExtractPlugin.loader,  //生产环境css文件分离loader
                    'css-loader'
                ]
            },
            {
                test: /\.svg$/,
                loader: 'svg-sprite-loader',
                include: path.resolve('src/assets/icons'),
                options: {
                    symbolId: 'icon-[name]'
                }
            },
            {
                test: /\.(png|svg|jpg|jpeg|gif)$/,
                exclude: path.resolve('src/assets/icons'),
                loader: 'file-loader',
                options: {
                    name: 'static/images/[name].[hash:7].[ext]',
                    esModule: false  //图片会导出为一个模块，禁止以模块导出为文件
                }
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                include: path.resolve('src'),
                loader: 'file-loader',
                options: {
                    name: 'static/font/[name].[hash:7].[ext]',
                    esModule: false
                }
            },
            {
                test:/\.html$/,
                include: path.resolve('src'),  //只在src下的文件执行loader
                loader:'html-loader'
            }
        ]
    }
}